package com.service;


import com.bean.sqlite.api.*;
import com.mapper.repository.mysql.BaseConfigRepository;
import com.mapper.repository.mysql.FreeApiInfoRepository;
import com.mapper.repository.sqlite.FreeApiRandomGoddessRepository;
import com.mapper.repository.sqlite.FreeApiRandomGoddessVideoRepository;
import com.mapper.repository.sqlite.FreeApiRandomRiddleRepository;
import com.mapper.sqlite.FreeApiJokesMapper;
import com.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class FreeApiLocalServiceImpl implements FreeApiLocalService {

    @Autowired
    private FreeApiJokesMapper freeApiJokesMapper;

    @Autowired
    private FreeApiService freeApiService;

    @Override
    public FreeApiJokes getJoke() {
        FreeApiJokes freeApiJokes = freeApiJokesMapper.randomJokes();
        return freeApiJokes;
    }

    //获取奇葩对话
    @Override
    public FreeApiQpdh getQpdh() {
        int result = ThreadLocalRandom.current().nextInt(2);
        if(result==0){//调用API
            FreeApiQpdh freeApiQpdh = new FreeApiQpdh();
            Map map = freeApiService.addQpdh();
            if((Integer) map.get("code")==200){
                freeApiQpdh.setContent(map.get("content").toString()+"\n");
            }
            return freeApiQpdh;
        }else{
            FreeApiQpdh freeApiQpdh = freeApiJokesMapper.randomQpdh();
            return freeApiQpdh;
        }
    }

    //获取脑筋急转弯
    @Override
    public FreeApiRandomRiddle getRiddle() {
        int result = ThreadLocalRandom.current().nextInt(2);
        if(result==0){//调用API
            FreeApiRandomRiddle freeApiRandomRiddle  = new FreeApiRandomRiddle();
            Map map = freeApiService.addRiddle();
            if((Integer) map.get("code")==200){
                freeApiRandomRiddle = (FreeApiRandomRiddle) map.get("riddle");
            }
            return freeApiRandomRiddle;
        }else{
            FreeApiRandomRiddle freeApiRandomRiddle = freeApiJokesMapper.randomRiddle();
            return freeApiRandomRiddle;
        }


    }

    @Override
    public FreeApiRandomTu getImg(String type) {
        int result = ThreadLocalRandom.current().nextInt(2);
        if(result==0){//调用API
            FreeApiRandomTu freeApiRandomTu = new FreeApiRandomTu();
            Map map = null;
            String url = null;
            switch (type){
                case "随机女神":
                    map = freeApiService.getRandomGoddess();
                    url = map.get("content").toString();
                    freeApiRandomTu.setContent(url);
                    break;
                default:
                    map = freeApiService.getRandomTu(type);
                    url = map.get("content").toString();
                    freeApiRandomTu.setContent(url);
                    break;
            }
            return freeApiRandomTu;
        }else{
            FreeApiRandomTu freeApiRandomTu = new FreeApiRandomTu();
            switch (type){
                case "随机女神":
                    freeApiRandomTu = freeApiJokesMapper.randomImgGoddess();
                    break;
                default:
                    freeApiRandomTu = freeApiJokesMapper.randomImg(type);
                    break;
            }

            return freeApiRandomTu;
        }

    }

    @Override
    public FreeApiRandomGoddessVideo getVideo() {
        int result = ThreadLocalRandom.current().nextInt(2);
        if(result==0){//调用API
            Map map = freeApiService.getRandomGoddessVideo();
            String url = map.get("content").toString();
            FreeApiRandomGoddessVideo freeApiRandomGoddessVideo =new FreeApiRandomGoddessVideo();
            freeApiRandomGoddessVideo.setContent(url);
            return freeApiRandomGoddessVideo;

        }else{
            return freeApiJokesMapper.randomVideo();
        }


    }


}
